package com.cskaoyan.service;

import com.cskaoyan.bean.domain.Category;
import com.cskaoyan.bean.domain.CategoryExample;
import com.cskaoyan.bean.wxcatalogs.WxCatalog.CatalogIndexVo;
import com.cskaoyan.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author looper
 * @
 **/
@Service
public class WxCatalogServiceImpl implements WxCatalogService {
    @Resource
    CategoryMapper mapper;

    @Override
    public CatalogIndexVo getIndex() {
        String level = "L1";
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        //返回所有L1的类目
        criteria.andLevelEqualTo(level).andDeletedEqualTo(false);
        List<Category> categoriesLevelTop = mapper.selectByExample(example);
        Integer id = null;
        try {
            //如果类目为空
            id = categoriesLevelTop.get(0).getId();
        } catch (Exception e) {
            return null;
        }
        //返回当前类目第一个id的类目
        Category category = mapper.selectByPrimaryKey(id);
        //返回所有pid为第一个id的子类目
        CategoryExample exampleSub = new CategoryExample();
        exampleSub.createCriteria().andPidEqualTo(id).andDeletedEqualTo(false);
        List<Category> categoriesSub = mapper.selectByExample(exampleSub);
        CatalogIndexVo catalogIndexVo = new CatalogIndexVo();
        catalogIndexVo.setCategoryList(categoriesLevelTop);
        catalogIndexVo.setCurrentCategory(category);
        catalogIndexVo.setCurrentSubCategory(categoriesSub);
        return catalogIndexVo;
    }

    @Override
    public CatalogIndexVo getCurrent(Integer id) {
        CatalogIndexVo catalogIndexVo = new CatalogIndexVo();
        //当前id的catalog
        Category category = mapper.selectByPrimaryKey(id);
        catalogIndexVo.setCurrentCategory(category);
        //与pid==id 的sub
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPidEqualTo(id);
        List<Category> categoriesSub = mapper.selectByExample(example);
        catalogIndexVo.setCurrentSubCategory(categoriesSub);
        //返回
        return catalogIndexVo;
    }
}
